home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 319 / compsrc1 / combine.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  61.7 KB  |  1,938 lines

  1. /* Optimize by combining instructions for GNU compiler.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. /* This module is essentially the "combiner" phase of the U. of Arizona
  23.    Portable Optimizer, but redone to work on our list-structured
  24.    representation for RTL instead of their string representation.
  25.  
  26.    The LOG_LINKS of each insn identify the most recent assignment
  27.    to each REG used in the insn.  It is a list of previous insns,
  28.    each of which contains a SET for a REG that is used in this insn
  29.    and not used or set in between.  LOG_LINKs never cross basic blocks.
  30.    They were set up by the preceding pass (lifetime analysis).
  31.  
  32.    We try to combine each pair of insns joined by a logical link.
  33.    We also try to combine triples of insns A, B and C when
  34.    C has a link back to B and B has a link back to A.
  35.  
  36.    LOG_LINKS does not have links for use of the CC0.  They don't
  37.    need to, because the insn that sets the CC0 is always immediately
  38.    before the insn that tests it.  So we always regard a branch
  39.    insn as having a logical link to the preceding insn.
  40.  
  41.    We check (with use_crosses_set_p) to avoid combining in such a way
  42.    as to move a computation to a place where its value would be different.
  43.  
  44.    Combination is done by mathematically substituting the previous
  45.    insn(s) values for the regs they set into the expressions in
  46.    the later insns that refer to these regs.  If the result is a valid insn
  47.    for our target machine, according to the machine description,
  48.    we install it, delete the earlier insns, and update the data flow
  49.    information (LOG_LINKS and REG_NOTES) for what we did.
  50.  
  51.    To simplify substitution, we combine only when the earlier insn(s)
  52.    consist of only a single assignment.  To simplify updating afterward,
  53.    we never combine when a subroutine call appears in the middle.
  54.  
  55.    Since we do not represent assignments to CC0 explicitly except when that
  56.    is all an insn does, there is no LOG_LINKS entry in an insn that uses
  57.    the condition code for the insn that set the condition code.
  58.    Fortunately, these two insns must be consecutive.
  59.    Therefore, every JUMP_INSN is taken to have an implicit logical link
  60.    to the preceding insn.  This is not quite right, since non-jumps can
  61.    also use the condition code; but in practice such insns would not
  62.    combine anyway.  */
  63.  
  64. #include "config.h"
  65. #include "rtl.h"
  66. #include "flags.h"
  67. #include "regs.h"
  68. #include "basic-block.h"
  69. #include "insn-config.h"
  70. #include "recog.h"
  71.  
  72. #define max(A,B) ((A) > (B) ? (A) : (B))
  73. #define min(A,B) ((A) < (B) ? (A) : (B))
  74.  
  75. /* It is not safe to use ordinary gen_lowpart in combine.
  76.    Use gen_lowpart_for_combine instead.  See comments there.  */
  77. #define gen_lowpart dont_use_gen_lowpart_you_dummy
  78.  
  79. /* Number of attempts to combine instructions in this function.  */
  80.  
  81. static int combine_attempts;
  82.  
  83. /* Number of attempts that got as far as substitution in this function.  */
  84.  
  85. static int combine_merges;
  86.  
  87. /* Number of instructions combined with added SETs in this function.  */
  88.  
  89. static int combine_extras;
  90.  
  91. /* Number of instructions combined in this function.  */
  92.  
  93. static int combine_successes;
  94.  
  95. /* Totals over entire compilation.  */
  96.  
  97. static int total_attempts, total_merges, total_extras, total_successes;
  98.  
  99.  
  100. /* Vector mapping INSN_UIDs to cuids.
  101.    The cuids are like uids but increase monononically always.
  102.    Combine always uses cuids so that it can compare them.
  103.    But actually renumbering the uids, which we used to do,
  104.    proves to be a bad idea because it makes it hard to compare
  105.    the dumps produced by earlier passes with those from later passes.  */
  106.  
  107. static short *uid_cuid;
  108.  
  109. /* Get the cuid of an insn.  */
  110.  
  111. #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
  112.  
  113.  
  114. /* Record last point of death of (hard or pseudo) register n.  */
  115.  
  116. static rtx *reg_last_death;
  117.  
  118. /* Record last point of modification of (hard or pseudo) register n.  */
  119.  
  120. static rtx *reg_last_set;
  121.  
  122. /* Record the cuid of the last insn that invalidated memory
  123.    (anything that writes memory, and subroutine calls).  */
  124.  
  125. static int mem_last_set;
  126.  
  127. /* Record the cuid of the last CALL_INSN
  128.    so we can tell whether a potential combination crosses any calls.  */
  129.  
  130. static int last_call_cuid;
  131.  
  132. /* When `subst' is called, this is the insn that is being modified
  133.    (by combining in a previous insn).  The PATTERN of this insn
  134.    is still the old pattern partially modified and it should not be
  135.    looked at, but this may be used to examine the successors of the insn
  136.    to judge whether a simplification is valid.  */
  137.  
  138. static rtx subst_insn;
  139.  
  140. /* Record one modification to rtl structure
  141.    to be undone by storing old_contents into *where.  */
  142.  
  143. struct undo
  144. {
  145.   rtx *where;
  146.   rtx old_contents;
  147. };
  148.  
  149. /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
  150.    num_undo says how many are currently recorded.
  151.    storage is nonzero if we must undo the allocation of new storage.
  152.    The value of storage is what to pass to obfree.  */
  153.  
  154. #define MAX_UNDO 10
  155.  
  156. struct undobuf
  157. {
  158.   int num_undo;
  159.   char *storage;
  160.   struct undo undo[MAX_UNDO];
  161. };
  162.  
  163. static struct undobuf undobuf;
  164.  
  165. /* Number of times the pseudo being substituted for
  166.    was found and replaced.  */
  167.  
  168. static int n_occurrences;
  169.  
  170. static void move_deaths ();
  171. void remove_death ();
  172. static void record_dead_and_set_regs ();
  173. int regno_dead_p ();
  174. static int use_crosses_set_p ();
  175. static int try_combine ();
  176. static rtx subst ();
  177. static void undo_all ();
  178. static void copy_substitutions ();
  179. static void add_links ();
  180. static void add_incs ();
  181. static int insn_has_inc_p ();
  182. static int adjacent_insns_p ();
  183. static rtx simplify_and_const_int ();
  184. static rtx gen_lowpart_for_combine ();
  185. static void simplify_set_cc0_and ();
  186.  
  187. /* Main entry point for combiner.  F is the first insn of the function.
  188.    NREGS is the first unused pseudo-reg number.  */
  189.  
  190. void
  191. combine_instructions (f, nregs)
  192.      rtx f;
  193.      int nregs;
  194. {
  195.   register rtx insn;
  196.   register int i;
  197.   register rtx links, nextlinks;
  198.   rtx prev;
  199.  
  200.   combine_attempts = 0;
  201.   combine_merges = 0;
  202.   combine_extras = 0;
  203.   combine_successes = 0;
  204.  
  205.   reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
  206.   reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
  207.   bzero (reg_last_death, nregs * sizeof (rtx));
  208.   bzero (reg_last_set, nregs * sizeof (rtx));
  209.  
  210.   init_recog ();
  211.  
  212.   /* Compute maximum uid value so uid_cuid can be allocated.  */
  213.  
  214.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  215.     if (INSN_UID (insn) > i)
  216.       i = INSN_UID (insn);
  217.  
  218.   uid_cuid = (short *) alloca ((i + 1) * sizeof (short));
  219.  
  220.   /* Compute the mapping from uids to cuids.
  221.      Cuids are numbers assigned to insns, like uids,
  222.      except that cuids increase monotonically through the code.  */
  223.  
  224.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  225.     INSN_CUID (insn) = ++i;
  226.  
  227.   /* Now scan all the insns in forward order.  */
  228.  
  229.   last_call_cuid = 0;
  230.   mem_last_set = 0;
  231.   prev = 0;
  232.  
  233.   for (insn = f; insn; insn = NEXT_INSN (insn))
  234.     {
  235.       if (GET_CODE (insn) == INSN
  236.       || GET_CODE (insn) == CALL_INSN
  237.       || GET_CODE (insn) == JUMP_INSN)
  238.     {
  239.     retry:
  240.       /* Try this insn with each insn it links back to.  */
  241.  
  242.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  243.         if (try_combine (insn, XEXP (links, 0), 0))
  244.           goto retry;
  245.  
  246.       /* Try each sequence of three linked insns ending with this one.  */
  247.  
  248.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  249.         if (GET_CODE (XEXP (links, 0)) != NOTE)
  250.           for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
  251.            nextlinks = XEXP (nextlinks, 1))
  252.         if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
  253.           goto retry;
  254.  
  255.       /* Try to combine a jump insn that uses CC0
  256.          with a preceding insn that sets CC0, and maybe with its
  257.          logical predecessor as well.
  258.          This is how we make decrement-and-branch insns.
  259.          We need this special code because data flow connections
  260.          via CC0 do not get entered in LOG_LINKS.  */
  261.  
  262.       if (GET_CODE (insn) == JUMP_INSN
  263.           && prev != 0
  264.           && GET_CODE (prev) == INSN
  265.           && GET_CODE (PATTERN (prev)) == SET
  266.           && GET_CODE (SET_DEST (PATTERN (prev))) == CC0)
  267.         {
  268.           if (try_combine (insn, prev, 0))
  269.           goto retry;
  270.  
  271.           if (GET_CODE (prev) != NOTE)
  272.         for (nextlinks = LOG_LINKS (prev); nextlinks;
  273.              nextlinks = XEXP (nextlinks, 1))
  274.           if (try_combine (insn, prev, XEXP (nextlinks, 0)))
  275.             goto retry;
  276.         }
  277. #if 0
  278. /* Turned off because on 68020 it takes four insns to make
  279.    something like (a[b / 32] & (1 << (31 - (b % 32)))) != 0
  280.    that could actually be optimized, and that's an unlikely piece of code.  */
  281.       /* If an insn gets or sets a bit field, try combining it
  282.          with two different insns whose results it uses.  */
  283.       if (GET_CODE (insn) == INSN
  284.           && GET_CODE (PATTERN (insn)) == SET
  285.           && (GET_CODE (SET_DEST (PATTERN (insn))) == ZERO_EXTRACT
  286.           || GET_CODE (SET_DEST (PATTERN (insn))) == SIGN_EXTRACT
  287.           || GET_CODE (SET_SRC (PATTERN (insn))) == ZERO_EXTRACT
  288.           || GET_CODE (SET_SRC (PATTERN (insn))) == SIGN_EXTRACT))
  289.         {
  290.           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  291.         if (GET_CODE (XEXP (links, 0)) != NOTE)
  292.           for (nextlinks = XEXP (links, 1); nextlinks;
  293.                nextlinks = XEXP (nextlinks, 1))
  294.             if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
  295.               goto retry;
  296.         }
  297. #endif
  298.       record_dead_and_set_regs (insn);
  299.       prev = insn;
  300.     }
  301.       else if (GET_CODE (insn) != NOTE)
  302.     prev = 0;
  303.     }
  304.   total_attempts += combine_attempts;
  305.   total_merges += combine_merges;
  306.   total_extras += combine_extras;
  307.   total_successes += combine_successes;
  308. }
  309.  
  310. /* Try to combine the insns I1 and I2 into I3.
  311.    Here I1 appears earlier than I2, which is earlier than I3.
  312.    I1 can be zero; then we combine just I2 into I3.
  313.  
  314.    Return 1 if successful; if that happens, I1 and I2 are pseudo-deleted
  315.    by turning them into NOTEs, and I3 is modified.
  316.    Return 0 if the combination does not work.  Then nothing is changed.  */
  317.  
  318. static int
  319. try_combine (i3, i2, i1)
  320.      register rtx i3, i2, i1;
  321. {
  322.   register rtx newpat;
  323.   int added_sets_1 = 0;
  324.   int added_sets_2 = 0;
  325.   int total_sets;
  326.   int i2_is_used;
  327.   register rtx link;
  328.   int insn_code_number;
  329.   int recog_flags = 0;
  330.   rtx i2dest, i2src;
  331.   rtx i1dest, i1src;
  332.   int maxreg;
  333.  
  334.   combine_attempts++;
  335.  
  336.   /* Don't combine with something already used up by combination.  */
  337.  
  338.   if (GET_CODE (i2) == NOTE
  339.       || (i1 && GET_CODE (i1) == NOTE))
  340.     return 0;
  341.  
  342.   /* Don't combine across a CALL_INSN, because that would possibly
  343.      change whether the life span of some REGs crosses calls or not,
  344.      and it is a pain to update that information.  */
  345.  
  346.   if (INSN_CUID (i2) < last_call_cuid
  347.       || (i1 && INSN_CUID (i1) < last_call_cuid))
  348.     return 0;
  349.  
  350.   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
  351.      That REG must be either set or dead by the final instruction
  352.      (so that we can safely forget about setting it).
  353.      Also test use_crosses_set_p to make sure that the value
  354.      that is to be substituted for the register
  355.      does not use any registers whose values alter in between.
  356.      Do not try combining with moves from one register to another
  357.      since it is better to let them be tied by register allocation.
  358.      (There is a switch to permit such combination; except the insns
  359.      that copy a function value into another register are never combined
  360.      because moving that too far away from the function call could cause
  361.      something else to be stored in that register in the interim.)
  362.  
  363.      A set of a SUBREG is considered as if it were a set from
  364.      SUBREG.  Thus, (SET (SUBREG:X (REG:Y...)) (something:X...))
  365.      is handled by substituting (SUBREG:Y (something:X...)) for (REG:Y...).  */
  366.  
  367.   if (GET_CODE (PATTERN (i2)) != SET)
  368.     return 0;
  369.   i2dest = SET_DEST (PATTERN (i2));
  370.   i2src = SET_SRC (PATTERN (i2));
  371.   if (GET_CODE (i2dest) == SUBREG)
  372.     {
  373.       i2dest = SUBREG_REG (i2dest);
  374.       i2src = gen_rtx (SUBREG, GET_MODE (i2dest), i2src, 0);
  375.     }
  376.   /* Don't eliminate a store in the stack pointer.  */
  377.   if (i2dest == stack_pointer_rtx)
  378.     return 0;
  379.   if (GET_CODE (i2dest) != CC0
  380.       && (GET_CODE (i2dest) != REG
  381.       || (GET_CODE (i2src) == REG
  382.           && (!flag_combine_regs
  383.           || FUNCTION_VALUE_REGNO_P (REGNO (i2src))))
  384.       || GET_CODE (i2src) == CALL
  385.       || use_crosses_set_p (i2src, INSN_CUID (i2))))
  386.     return 0;
  387.  
  388.   if (i1 != 0)
  389.     {
  390.       if (GET_CODE (PATTERN (i1)) != SET)
  391.     return 0;
  392.       i1dest = SET_DEST (PATTERN (i1));
  393.       i1src = SET_SRC (PATTERN (i1));
  394.       if (GET_CODE (i1dest) == SUBREG)
  395.     {
  396.       i1dest = SUBREG_REG (i1dest);
  397.       i1src = gen_rtx (SUBREG, GET_MODE (i1dest), i1src, 0);
  398.     }
  399.       if (i1dest == stack_pointer_rtx)
  400.     return 0;
  401.       if (GET_CODE (i1dest) != CC0
  402.       && (GET_CODE (i1dest) != REG
  403.           || (GET_CODE (i1src) == REG
  404.           && (!flag_combine_regs
  405.               || FUNCTION_VALUE_REGNO_P (REGNO (i1src))))
  406.           || GET_CODE (i1src) == CALL
  407.           || use_crosses_set_p (i1src, INSN_CUID (i1))))
  408.     return 0;
  409.     }
  410.  
  411.   /* If I1 or I2 contains an autoincrement or autodecrement,
  412.      make sure that register is not used between there and I3.
  413.      Also insist that I3 not be a jump; if it were one
  414.      and the incremented register were spilled, we would lose.  */
  415.   if ((link = find_reg_note (i2, REG_INC, 0)) != 0
  416.       && (GET_CODE (i3) == JUMP_INSN
  417.       || reg_used_between_p (XEXP (link, 0), i2, i3)
  418.       || reg_mentioned_p (XEXP (link, 0), i3)))
  419.     return 0;
  420.  
  421.   if (i1 && (link = find_reg_note (i1, REG_INC, 0)) != 0
  422.       && (GET_CODE (i3) == JUMP_INSN
  423.       || reg_used_between_p (XEXP (link, 0), i1, i3)
  424.       || reg_mentioned_p (XEXP (link, 0), i3)))
  425.     return 0;
  426.  
  427.   /* See if the SETs in i1 or i2 need to be kept around in the merged
  428.      instruction: whenever the value set there is still needed past i3.  */
  429.   added_sets_2 = (GET_CODE (i2dest) != CC0
  430.           && ! dead_or_set_p (i3, i2dest));
  431.   if (i1)
  432.     added_sets_1 = ! (dead_or_set_p (i3, i1dest)
  433.               || dead_or_set_p (i2, i1dest));
  434.  
  435.   combine_merges++;
  436.  
  437.   undobuf.num_undo = 0;
  438.   undobuf.storage = 0;
  439.  
  440.   /* Substitute in the latest insn for the regs set by the earlier ones.  */
  441.  
  442.   maxreg = max_reg_num ();
  443.  
  444.   subst_insn = i3;
  445.   n_occurrences = 0;        /* `subst' counts here */
  446.  
  447.   newpat = subst (PATTERN (i3), i2dest, i2src);
  448.   /* Record whether i2's body now appears within i3's body.  */
  449.   i2_is_used = n_occurrences;
  450.  
  451.   if (i1)
  452.     {
  453.       n_occurrences = 0;
  454.       newpat = subst (newpat, i1dest, i1src);
  455.     }
  456.  
  457.   if (GET_CODE (PATTERN (i3)) == SET
  458.       && SET_DEST (PATTERN (i3)) == cc0_rtx
  459.       && (GET_CODE (SET_SRC (PATTERN (i3))) == AND
  460.       || GET_CODE (SET_SRC (PATTERN (i3))) == LSHIFTRT)
  461.       && next_insn_tests_no_inequality (i3))
  462.     simplify_set_cc0_and (i3);
  463.  
  464.   if (max_reg_num () != maxreg)
  465.     abort ();
  466.  
  467.   /* If the actions of the earler insns must be kept
  468.      in addition to substituting them into the latest one,
  469.      we must make a new PARALLEL for the latest insn
  470.      to hold additional the SETs.  */
  471.  
  472.   if (added_sets_1 || added_sets_2)
  473.     {
  474.       combine_extras++;
  475.  
  476.       /* Arrange to free later what we allocate now
  477.      if we don't accept this combination.  */
  478.       if (!undobuf.storage)
  479.     undobuf.storage = (char *) oballoc (0);
  480.  
  481.       if (GET_CODE (newpat) == PARALLEL)
  482.     {
  483.       rtvec old = XVEC (newpat, 0);
  484.       total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
  485.       newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  486.       bcopy (&old->elem[0], &XVECEXP (newpat, 0, 0),
  487.          sizeof (old->elem[0]) * old->num_elem);
  488.     }
  489.       else
  490.     {
  491.       rtx old = newpat;
  492.       total_sets = 1 + added_sets_1 + added_sets_2;
  493.       newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  494.       XVECEXP (newpat, 0, 0) = old;
  495.     }
  496.      if (added_sets_1)
  497.     {
  498.       XVECEXP (newpat, 0, --total_sets) = PATTERN (i1);
  499.     }
  500.      if (added_sets_2)
  501.     {
  502.       /* If there is no I1, use I2's body as is.  */
  503.       if (i1 == 0
  504.       /* If I2 was stuck into I3, then anything within it has
  505.          already had I1 substituted into it when that was done to I3.  */
  506.           || i2_is_used)
  507.         {
  508.           XVECEXP (newpat, 0, --total_sets) = PATTERN (i2);
  509.         }
  510.       else
  511.         XVECEXP (newpat, 0, --total_sets)
  512.           = subst (PATTERN (i2), i1dest, i1src);
  513.     }
  514.     }
  515.  
  516.   /* Fail if an autoincrement side-effect has been duplicated.  */
  517.   if ((i2_is_used > 1 && find_reg_note (i2, REG_INC, 0) != 0)
  518.       || (i1 != 0 && n_occurrences > 1 && find_reg_note (i1, REG_INC, 0) != 0))
  519.     {
  520.       undo_all ();
  521.       return 0;
  522.     }
  523.  
  524.   /* Is the result of combination a valid instruction?  */
  525.   insn_code_number = recog (newpat, i3);
  526.  
  527.   if (insn_code_number >= 0)
  528.     {
  529.       /* Yes.  Install it.  */
  530.       register int regno;
  531.       INSN_CODE (i3) = insn_code_number;
  532.       PATTERN (i3) = newpat;
  533.       /* If anything was substituted more than once,
  534.      copy it to avoid invalid shared rtl structure.  */
  535.       copy_substitutions ();
  536.       /* The data flowing into I2 now flows into I3.
  537.      But we cannot always move all of I2's LOG_LINKS into I3,
  538.      since they must go to a setting of a REG from the
  539.      first use following.  If I2 was the first use following a set,
  540.      I3 is now a use, but it is not the first use
  541.      if some instruction between I2 and I3 is also a use.
  542.      Here, for simplicity, we move all the links only if
  543.      there are no real insns between I2 and I3.
  544.      Otherwise, we move only links that correspond to regs
  545.      that used to die in I2.  They are always safe to move.  */
  546.       add_links (i3, i2, adjacent_insns_p (i2, i3));
  547.       /* Most REGs that previously died in I2 now die in I3.  */ 
  548.       move_deaths (i2src, INSN_CUID (i2), i3);
  549.       if (GET_CODE (i2dest) == REG)
  550.     {
  551.       /* If the reg formerly set in I2 died only once and that was in I3,
  552.          zero its use count so it won't make `reload' do any work.  */
  553.       regno = REGNO (i2dest);
  554.       if (! added_sets_2)
  555.         {
  556.           reg_n_sets[regno]--;
  557.           /* Used to check  && regno_dead_p (regno, i3)  also here.  */
  558.           if (reg_n_sets[regno] == 0
  559.           && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  560.             & (1 << (regno % HOST_BITS_PER_INT))))
  561.         reg_n_refs[regno] = 0;
  562.         }
  563.       /* If a ref to REGNO was substituted into I3 from I2,
  564.          then it still dies there if it previously did.
  565.          Otherwise either REGNO never did die in I3 so remove_death is safe
  566.          or this entire life of REGNO is gone so remove its death.  */
  567.       if (!added_sets_2
  568.           && ! reg_mentioned_p (i2dest, PATTERN (i3)))
  569.         remove_death (regno, i3);
  570.     }
  571.       /* Any registers previously autoincremented in I2
  572.      are now incremented in I3.  */
  573.       add_incs (i3, REG_NOTES (i2));
  574.       if (i1)
  575.     {
  576.       /* Likewise, merge the info from I1 and get rid of it.  */
  577.       add_links (i3, i1,
  578.              adjacent_insns_p (i1, i2) && adjacent_insns_p (i2, i3));
  579.       move_deaths (i1src, INSN_CUID (i1), i3);
  580.       if (GET_CODE (i1dest) == REG)
  581.         {
  582.           regno = REGNO (i1dest);
  583.           if (! added_sets_1)
  584.         {
  585.           reg_n_sets[regno]--;
  586.           /* Used to also check  && regno_dead_p (regno, i3) here.  */
  587.  
  588.           if (reg_n_sets[regno] == 0
  589.               && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  590.                 & (1 << (regno % HOST_BITS_PER_INT))))
  591.  
  592.             reg_n_refs[regno] = 0;
  593.         }
  594.           /* If a ref to REGNO was substituted into I3 from I1,
  595.          then it still dies there if it previously did.
  596.          Else either REGNO never did die in I3 so remove_death is safe
  597.          or this entire life of REGNO is gone so remove its death.  */
  598.           if (! added_sets_1
  599.           && ! reg_mentioned_p (i1dest, PATTERN (i3)))
  600.         remove_death (regno, i3);
  601.         }
  602.       add_incs (i3, REG_NOTES (i1));
  603.       LOG_LINKS (i1) = 0;
  604.       PUT_CODE (i1, NOTE);
  605.       NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
  606.       NOTE_SOURCE_FILE (i1) = 0;
  607.     }
  608.       /* Get rid of I2.  */
  609.       LOG_LINKS (i2) = 0;
  610.       PUT_CODE (i2, NOTE);
  611.       NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
  612.       NOTE_SOURCE_FILE (i2) = 0;
  613.  
  614.       combine_successes++;
  615.       return 1;
  616.     }
  617.  
  618.   /* Failure: change I3 back the way it was.  */
  619.   undo_all ();
  620.  
  621.   return 0;
  622. }
  623.  
  624. /* Undo all the modifications recorded in undobuf.  */
  625.  
  626. static void
  627. undo_all ()
  628. {
  629.   register int i;
  630.   if (undobuf.num_undo > MAX_UNDO)
  631.     undobuf.num_undo = MAX_UNDO;
  632.   for (i = undobuf.num_undo - 1; i >= 0; i--)
  633.     *undobuf.undo[i].where = undobuf.undo[i].old_contents;
  634.   if (undobuf.storage)
  635.     obfree (undobuf.storage);
  636.   undobuf.num_undo = 0;
  637.   undobuf.storage = 0;
  638. }
  639.  
  640. /* If this insn had more than one substitution,
  641.    copy all but one, so that no invalid shared substructure is introduced.  */
  642.  
  643. static void
  644. copy_substitutions ()
  645. {
  646.   register int i;
  647.   if (undobuf.num_undo > 1)
  648.     {
  649.       for (i = undobuf.num_undo - 1; i >= 1; i--)
  650.     *undobuf.undo[i].where = copy_rtx (*undobuf.undo[i].where);
  651.     }
  652. }
  653.  
  654. /* Throughout X, replace FROM with TO, and return the result.
  655.    The result is TO if X is FROM;
  656.    otherwise the result is X, but its contents may have been modified.
  657.    If they were modified, a record was made in undobuf so that
  658.    undo_all will (among other things) return X to its original state.
  659.  
  660.    If the number of changes necessary is too much to record to undo,
  661.    the excess changes are not made, so the result is invalid.
  662.    The changes already made can still be undone.
  663.    undobuf.num_undo is incremented for such changes, so by testing that
  664.    the caller can tell whether the result is valid.
  665.  
  666.    `n_occurrences' is incremented each time FROM is replaced.  */
  667.  
  668. static rtx
  669. subst (x, from, to)
  670.      register rtx x, from, to;
  671. {
  672.   register char *fmt;
  673.   register int len, i;
  674.   register enum rtx_code code;
  675. #ifdef atarist
  676.   short was_replaced[2];    /* 'char' confuses GAS 1.14... */
  677. #else
  678.   char was_replaced[2];
  679. #endif
  680.  
  681. #define SUBST(INTO, NEWVAL)  \
  682.  do { if (undobuf.num_undo < MAX_UNDO)                    \
  683.     {                                \
  684.       undobuf.undo[undobuf.num_undo].where = &INTO;            \
  685.       undobuf.undo[undobuf.num_undo].old_contents = INTO;        \
  686.       INTO = NEWVAL;                        \
  687.     }                                \
  688.       undobuf.num_undo++; } while (0)
  689.  
  690. /* FAKE_EXTEND_SAFE_P (MODE, FROM) is 1 if (subreg:MODE FROM 0) is a safe
  691.    replacement for (zero_extend:MODE FROM) or (sign_extend:MODE FROM).
  692.    If it is 0, that cannot be done because it might cause a badly aligned
  693.    memory reference.  */
  694.  
  695. /* Now we never do this for memory refs, because of the danger of
  696.    turning a reference to the last byte on a page into a page-crossing
  697.    reference that could get a spurious fault.  It could be done safely
  698.    for certain cases but it's hard to check for them.  */
  699. #if 0
  700. #define FAKE_EXTEND_SAFE_P(MODE, FROM)  \
  701.    (GET_CODE (FROM) == REG ||                \
  702.     (GET_CODE (FROM) == MEM                \
  703.      && offsetable_address_p ((MODE), XEXP ((FROM), 0))    \
  704.      && ! mode_dependent_address_p ((XEXP ((FROM), 0)))))
  705. #else
  706. #define FAKE_EXTEND_SAFE_P(MODE, FROM) (GET_CODE (FROM) == REG)
  707. #endif
  708.  
  709.   if (x == from)
  710.     return to;
  711.  
  712.   /* It is possible to have a subexpression appear twice in the insn.
  713.      Suppose that FROM is a register that appears within TO.
  714.      Then, after that subexpression has been scanned once by `subst',
  715.      the second time it is scanned, TO may be found.  If we were
  716.      to scan TO here, we would find FROM within it and create a
  717.      self-referent rtl structure which is completely wrong.  */
  718.   if (x == to)
  719.     return to;
  720.  
  721.   code = GET_CODE (x);
  722.  
  723.   /* A little bit of algebraic simplification here.  */
  724.   switch (code)
  725.     {
  726.       /* This case has no effect except to speed things up.  */
  727.     case REG:
  728.     case CONST_INT:
  729.     case CONST:
  730.     case SYMBOL_REF:
  731.     case LABEL_REF:
  732.     case PC:
  733.     case CC0:
  734.       return x;
  735.     }
  736.  
  737.   was_replaced[0] = 0;
  738.   was_replaced[1] = 0;
  739.  
  740.   len = GET_RTX_LENGTH (code);
  741.   fmt = GET_RTX_FORMAT (code);
  742.  
  743.   /* Don't replace FROM where it is being stored in rather than used.  */
  744.   if (code == SET && SET_DEST (x) == from)
  745.     fmt = "ie";
  746.   if (code == SET && GET_CODE (SET_DEST (x)) == SUBREG
  747.       && SUBREG_REG (SET_DEST (x)) == from)
  748.     fmt = "ie";
  749.  
  750.   for (i = 0; i < len; i++)
  751.     {
  752.       if (fmt[i] == 'E')
  753.     {
  754.       register int j;
  755.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  756.         {
  757.           register rtx new;
  758.           if (XVECEXP (x, i, j) == from)
  759.         new = to, n_occurrences++;
  760.           else
  761.         new = subst (XVECEXP (x, i, j), from, to);
  762.           if (new != XVECEXP (x, i, j))
  763.         SUBST (XVECEXP (x, i, j), new);
  764.         }
  765.     }
  766.       else if (fmt[i] == 'e')
  767.     {
  768.       register rtx new;
  769.  
  770.       if (XEXP (x, i) == from)
  771.         {
  772.           new = to;
  773.           n_occurrences++;
  774.           if (i < 2)
  775.         was_replaced[i] = 1;
  776.         }
  777.       else
  778.         new = subst (XEXP (x, i), from, to);
  779.  
  780.       if (new != XEXP (x, i))
  781.         SUBST (XEXP (x, i), new);
  782.     }
  783.     }
  784.  
  785.   /* A little bit of algebraic simplification here.  */
  786.   switch (code)
  787.     {
  788.     case SUBREG:
  789.       /* Changing mode twice with SUBREG => just change it once,
  790.      or not at all if changing back to starting mode.  */
  791.       if (SUBREG_REG (x) == to
  792.       && GET_CODE (to) == SUBREG
  793.       && SUBREG_WORD (x) == 0
  794.       && SUBREG_WORD (to) == 0)
  795.     {
  796.       if (GET_MODE (x) == GET_MODE (SUBREG_REG (to)))
  797.         return SUBREG_REG (to);
  798.       SUBST (SUBREG_REG (x), SUBREG_REG (to));
  799.     }
  800.       /* (subreg (sign_extend X)) is X, if it has same mode as X.  */
  801.       if (SUBREG_REG (x) == to
  802.       && (GET_CODE (to) == SIGN_EXTEND || GET_CODE (to) == ZERO_EXTEND)
  803.       && SUBREG_WORD (x) == 0
  804.       && GET_MODE (x) == GET_MODE (XEXP (to, 0)))
  805.     return XEXP (to, 0);
  806.       break;
  807.  
  808.     case NOT:
  809.       /* (not (minus X 1)) can become (neg X).  */
  810.       if (was_replaced[0]
  811.       && ((GET_CODE (to) == PLUS && INTVAL (XEXP (to, 1)) == -1)
  812.           || (GET_CODE (to) == MINUS && XEXP (to, 1) == const1_rtx)))
  813.     return gen_rtx (NEG, GET_MODE (to), XEXP (to, 0));
  814.       /* Don't let substitution introduce double-negatives.  */
  815.       if (was_replaced[0]
  816.           && GET_CODE (to) == code)
  817.         return XEXP (to, 0);
  818.       break;
  819.  
  820.     case NEG:
  821.       /* (neg (minus X Y)) can become (minus Y X).  */
  822.       if (was_replaced[0] && GET_CODE (to) == MINUS)
  823.           return gen_rtx (MINUS, GET_MODE (to),
  824.                           XEXP (to, 1), XEXP (to, 0));
  825.       /* Don't let substitution introduce double-negatives.  */
  826.       if (was_replaced[0]
  827.       && GET_CODE (to) == code)
  828.     return XEXP (to, 0);
  829.       break;
  830.  
  831.     case FLOAT_TRUNCATE:
  832.       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
  833.       if (was_replaced[0]
  834.       && GET_CODE (to) == FLOAT_EXTEND
  835.       && GET_MODE (XEXP (to, 0)) == GET_MODE (x))
  836.     return XEXP (to, 0);
  837.       break;
  838.  
  839.     case PLUS:
  840.       /* In (plus <foo> (ashift <bar> <n>))
  841.      change the shift to a multiply so we can recognize
  842.      scaled indexed addresses.  */
  843.       if ((was_replaced[0]
  844.        || was_replaced[1])
  845.       && GET_CODE (to) == ASHIFT
  846.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  847.       && INTVAL (XEXP (to, 1)) < HOST_BITS_PER_INT)
  848.     {
  849.       rtx temp;
  850.       if (!undobuf.storage)
  851.         undobuf.storage = (char *) oballoc (0);
  852.       temp = gen_rtx (MULT, GET_MODE (to),
  853.               XEXP (to, 0),
  854.               gen_rtx (CONST_INT, VOIDmode,
  855.                    1 << INTVAL (XEXP (to, 1))));
  856.       if (was_replaced[0])
  857.         SUBST (XEXP (x, 0), temp);
  858.       else
  859.         SUBST (XEXP (x, 1), temp);
  860.     }
  861.       /* (plus X (neg Y)) becomes (minus X Y).  */
  862.       if (GET_CODE (XEXP (x, 1)) == NEG)
  863.     {
  864.       if (!undobuf.storage)
  865.         undobuf.storage = (char *) oballoc (0);
  866.       return gen_rtx (MINUS, GET_MODE (x),
  867.               XEXP (x, 0), XEXP (XEXP (x, 1), 0));
  868.     }
  869.       /* (plus (neg X) Y) becomes (minus Y X).  */
  870.       if (GET_CODE (XEXP (x, 0)) == NEG)
  871.     {
  872.       if (!undobuf.storage)
  873.         undobuf.storage = (char *) oballoc (0);
  874.       return gen_rtx (MINUS, GET_MODE (x),
  875.               XEXP (x, 1), XEXP (XEXP (x, 0), 0));
  876.     }
  877.       /* (plus (plus x c1) c2) => (plus x c1+c2) */
  878.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  879.       && GET_CODE (XEXP (x, 0)) == PLUS
  880.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
  881.     {
  882.       int sum = (INTVAL (XEXP (x, 1))
  883.              + INTVAL (XEXP (XEXP (x, 0), 1)));
  884.       if (sum == 0)
  885.         return XEXP (XEXP (x, 0), 0);
  886.       if (!undobuf.storage)
  887.         undobuf.storage = (char *) oballoc (0);
  888.       SUBST (XEXP (x, 1), gen_rtx (CONST_INT, VOIDmode, sum));
  889.       SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  890.       break;
  891.     }
  892.       /* If we have something (putative index) being added to a sum,
  893.      associate it so that any constant term is outermost.
  894.      That's because that's the way indexed addresses are
  895.      now supposed to appear.  */
  896.       if (((was_replaced[0] && GET_CODE (XEXP (x, 1)) == PLUS)
  897.        || (was_replaced[1] && GET_CODE (XEXP (x, 0)) == PLUS))
  898.       ||
  899.       ((was_replaced[0] || was_replaced[1])
  900.        && GET_CODE (to) == PLUS))
  901.     {
  902.       rtx offset = 0, base, index;
  903.       if (GET_CODE (to) != PLUS)
  904.         {
  905.           index = to;
  906.           base = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
  907.         }
  908.       else
  909.         {
  910.           index = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
  911.           base = to;
  912.         }
  913.       if (CONSTANT_ADDRESS_P (XEXP (base, 0)))
  914.         {
  915.           offset = XEXP (base, 0);
  916.           base = XEXP (base, 1);
  917.         }
  918.       else if (CONSTANT_ADDRESS_P (XEXP (base, 1)))
  919.         {
  920.           offset = XEXP (base, 1);
  921.           base = XEXP (base, 0);
  922.         }
  923.       if (offset != 0)
  924.         {
  925.           if (!undobuf.storage)
  926.         undobuf.storage = (char *) oballoc (0);
  927.           if (GET_CODE (offset) == CONST_INT)
  928.         return plus_constant (gen_rtx (PLUS, GET_MODE (index),
  929.                            base, index),
  930.                       INTVAL (offset));
  931.           if (GET_CODE (index) == CONST_INT)
  932.         return plus_constant (gen_rtx (PLUS, GET_MODE (offset),
  933.                            base, offset),
  934.                       INTVAL (index));
  935.           return gen_rtx (PLUS, GET_MODE (index),
  936.                   gen_rtx (PLUS, GET_MODE (index),
  937.                        base, index),
  938.                   offset);
  939.         }
  940.     }
  941.       break;
  942.  
  943.     case MINUS:
  944.       /* Can simplify (minus:VOIDmode (zero/sign_extend FOO) CONST)
  945.      (which is a compare instruction, not a subtract instruction)
  946.      to (minus FOO CONST) if CONST fits in FOO's mode
  947.      and we are only testing equality.
  948.      In fact, this is valid for zero_extend if what follows is an
  949.      unsigned comparison, and for sign_extend with a signed comparison.  */
  950.       if (GET_MODE (x) == VOIDmode
  951.       && was_replaced[0]
  952.       && (GET_CODE (to) == ZERO_EXTEND || GET_CODE (to) == SIGN_EXTEND)
  953.       && next_insn_tests_no_inequality (subst_insn)
  954.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  955.       /* This is overly cautious by one bit, but saves worrying about
  956.          whether it is zero-extension or sign extension.  */
  957.       && ((unsigned) INTVAL (XEXP (x, 1))
  958.           < (1 << (GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0))) - 1))))
  959.     SUBST (XEXP (x, 0), XEXP (to, 0));
  960.       break;
  961.  
  962.     case EQ:
  963.     case NE:
  964.       /* If comparing a subreg against zero, discard the subreg.  */
  965.       if (was_replaced[0]
  966.       && GET_CODE (to) == SUBREG
  967.       && SUBREG_WORD (to) == 0
  968.       && XEXP (x, 1) == const0_rtx)
  969.     SUBST (XEXP (x, 0), SUBREG_REG (to));
  970.  
  971.       /* If comparing a ZERO_EXTRACT against zero,
  972.      canonicalize to a SIGN_EXTRACT,
  973.      since the two are equivalent here.  */
  974.       if (was_replaced[0]
  975.       && GET_CODE (to) == ZERO_EXTRACT
  976.       && XEXP (x, 1) == const0_rtx)
  977.     {
  978.       if (!undobuf.storage)
  979.         undobuf.storage = (char *) oballoc (0);
  980.       SUBST (XEXP (x, 0),
  981.          gen_rtx (SIGN_EXTRACT, GET_MODE (to),
  982.               XEXP (to, 0), XEXP (to, 1),
  983.               XEXP (to, 2)));
  984.     }
  985.       /* If we are putting (ASHIFT 1 x) into (EQ (AND ... y) 0),
  986.      arrange to return (EQ (SIGN_EXTRACT y 1 x) 0),
  987.      which is what jump-on-bit instructions are written with.  */
  988.       else if (XEXP (x, 1) == const0_rtx
  989.            && GET_CODE (XEXP (x, 0)) == AND
  990.            && (XEXP (XEXP (x, 0), 0) == to
  991.            || XEXP (XEXP (x, 0), 1) == to)
  992.            && GET_CODE (to) == ASHIFT
  993.            && XEXP (to, 0) == const1_rtx)
  994.     {
  995.       register rtx y = XEXP (XEXP (x, 0),
  996.                  XEXP (XEXP (x, 0), 0) == to);
  997.       if (!undobuf.storage)
  998.         undobuf.storage = (char *) oballoc (0);
  999.       SUBST (XEXP (x, 0),
  1000.          gen_rtx (SIGN_EXTRACT, GET_MODE (to),
  1001.               y,
  1002.               const1_rtx, XEXP (to, 1)));
  1003.     }
  1004.  
  1005.       break;
  1006.  
  1007.     case ZERO_EXTEND:
  1008.       if (was_replaced[0]
  1009.       && GET_CODE (to) == ZERO_EXTEND)
  1010.     SUBST (XEXP (x, 0), XEXP (to, 0));
  1011.       /* Zero-extending the result of an and with a constant can be done
  1012.      with a wider and.  */
  1013.       if (was_replaced[0]
  1014.       && GET_CODE (to) == AND
  1015.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1016.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1017.       /* Avoid getting wrong result if the constant has high bits set
  1018.          that are irrelevant in the narrow mode where it is being used.  */
  1019.       && 0 == (INTVAL (XEXP (to, 1))
  1020.            & ~ GET_MODE_MASK (GET_MODE (to))))
  1021.     {
  1022.       if (!undobuf.storage)
  1023.         undobuf.storage = (char *) oballoc (0);
  1024.       return gen_rtx (AND, GET_MODE (x),
  1025.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1026.               XEXP (to, 1));
  1027.     } 
  1028.       /* Change (zero_extend:M (subreg:N (zero_extract:M ...) 0))
  1029.      to (zero_extract:M ...) if the field extracted fits in mode N.  */
  1030.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1031.       && GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTRACT
  1032.       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
  1033.       && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
  1034.           <= GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
  1035.     {
  1036.       return XEXP (XEXP (x, 0), 0);
  1037.     }
  1038.       break;
  1039.  
  1040.     case SIGN_EXTEND:
  1041.       if (was_replaced[0]
  1042.       && GET_CODE (to) == SIGN_EXTEND)
  1043.     SUBST (XEXP (x, 0), XEXP (to, 0));
  1044.       /* Sign-extending the result of an and with a constant can be done
  1045.      with a wider and, provided the high bit of the constant is 0.  */
  1046.       if (was_replaced[0]
  1047.       && GET_CODE (to) == AND
  1048.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1049.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1050.       && ((INTVAL (XEXP (to, 1))
  1051.            & (-1 << (GET_MODE_BITSIZE (GET_MODE (to)) - 1)))
  1052.           == 0))
  1053.     {
  1054.       if (!undobuf.storage)
  1055.         undobuf.storage = (char *) oballoc (0);
  1056.       return gen_rtx (AND, GET_MODE (x),
  1057.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1058.               XEXP (to, 1));
  1059.      } 
  1060.       break;
  1061.  
  1062.     case SET:
  1063.       /* In (set (zero-extract <x> <n> <y>) (and <foo> <(2**n-1) | anything>))
  1064.      the `and' can be deleted.  This can happen when storing a bit
  1065.      that came from a set-flag insn followed by masking to one bit.  */
  1066.       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  1067.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1068.       && was_replaced[1]
  1069.       && GET_CODE (to) == AND
  1070.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1071.       && 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
  1072.            & ~ INTVAL (XEXP (to, 1))))
  1073.     {
  1074.       SUBST (XEXP (x, 1), XEXP (to, 0));
  1075.     } 
  1076.       /* In (set (zero-extract <x> <n> <y>)
  1077.              (subreg (and <foo> <(2**n-1) | anything>)))
  1078.      the `and' can be deleted.  */
  1079.       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  1080.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1081.       && GET_CODE (XEXP (x, 1)) == SUBREG
  1082.       && SUBREG_WORD (XEXP (x, 1)) == 0
  1083.       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == AND
  1084.       && GET_CODE (XEXP (SUBREG_REG (XEXP (x, 1)), 1)) == CONST_INT
  1085.       && 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
  1086.            & ~ INTVAL (XEXP (SUBREG_REG (XEXP (x, 1)), 1))))
  1087.     {
  1088.       SUBST (SUBREG_REG (XEXP (x, 1)), XEXP (SUBREG_REG (XEXP (x, 1)), 0));
  1089.     } 
  1090.       /* (set (zero_extract ...) (and/or/xor (zero_extract ...) const)),
  1091.      if both zero_extracts have the byte size and position,
  1092.      can be changed to avoid the byte extracts.  */
  1093.       if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  1094.        || GET_CODE (XEXP (x, 0)) == SIGN_EXTRACT)
  1095.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1096.       && (GET_CODE (XEXP (x, 1)) == AND
  1097.           || GET_CODE (XEXP (x, 1)) == IOR
  1098.           || GET_CODE (XEXP (x, 1)) == XOR)
  1099.       && (GET_CODE (XEXP (XEXP (x, 1), 0)) == ZERO_EXTRACT
  1100.           || GET_CODE (XEXP (XEXP (x, 1), 0)) == SIGN_EXTRACT)
  1101.       && rtx_equal_p (XEXP (XEXP (XEXP (x, 1), 0), 1),
  1102.               XEXP (XEXP (x, 0), 1))
  1103.       && rtx_equal_p (XEXP (XEXP (XEXP (x, 1), 0), 2),
  1104.               XEXP (XEXP (x, 0), 2))
  1105.       && GET_CODE (XEXP (XEXP (x, 1), 0)) == GET_CODE (XEXP (x, 0))
  1106.       && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
  1107.     {
  1108. #ifdef BITS_BIG_ENDIAN
  1109.       int shiftcount
  1110.         = GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
  1111.           - INTVAL (XEXP (XEXP (x, 0), 1)) - INTVAL (XEXP (XEXP (x, 0), 2));
  1112. #else
  1113.       int shiftcount
  1114.         = INTVAL (XEXP (XEXP (x, 0), 2));
  1115. #endif
  1116.       if (!undobuf.storage)
  1117.         undobuf.storage = (char *) oballoc (0);
  1118.       return
  1119.         gen_rtx (SET, VOIDmode,
  1120.              XEXP (XEXP (x, 0), 0),
  1121.              gen_rtx (GET_CODE (XEXP (x, 1)),
  1122.                   GET_MODE (XEXP (XEXP (x, 0), 0)),
  1123.                   XEXP (XEXP (XEXP (x, 1), 0), 0),
  1124.                   gen_rtx (CONST_INT, VOIDmode,
  1125.                        (INTVAL (XEXP (XEXP (x, 1), 1))
  1126.                     << shiftcount)
  1127.                        + (GET_CODE (XEXP (x, 1)) == AND
  1128.                       ? (1 << shiftcount) - 1
  1129.                       : 0))));
  1130.     }
  1131.       break;
  1132.  
  1133.     case AND:
  1134.       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  1135.     {
  1136.       rtx tem = simplify_and_const_int (x, to);
  1137.       if (tem)
  1138.         return tem;
  1139.     }
  1140.       break;
  1141.  
  1142.     case FLOAT:
  1143.       /* (float (sign_extend <X>)) = (float <X>).  */
  1144.       if (was_replaced[0]
  1145.       && GET_CODE (to) == SIGN_EXTEND)
  1146.     SUBST (XEXP (x, 0), XEXP (to, 0));
  1147.       break;
  1148.  
  1149.     case ZERO_EXTRACT:
  1150.       /* (ZERO_EXTRACT (TRUNCATE x)...)
  1151.      can become (ZERO_EXTRACT x ...).  */
  1152.       if (was_replaced[0]
  1153.       && GET_CODE (to) == TRUNCATE)
  1154.     {
  1155. #ifdef BITS_BIG_ENDIAN
  1156.       if (GET_CODE (XEXP (x, 2)) == CONST_INT)
  1157.         {
  1158.           if (!undobuf.storage)
  1159.         undobuf.storage = (char *) oballoc (0);
  1160.           /* On a big-endian machine, must increment the bit-number
  1161.          since sign bit is farther away in the pre-truncated value.  */
  1162.           return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
  1163.                   XEXP (to, 0),
  1164.                   XEXP (x, 1),
  1165.                   gen_rtx (CONST_INT, VOIDmode,
  1166.                        (INTVAL (XEXP (x, 2))
  1167.                     + GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0)))
  1168.                     - GET_MODE_BITSIZE (GET_MODE (to)))));
  1169.         }
  1170. #else
  1171.       SUBST (XEXP (x, 0), XEXP (to, 0));
  1172. #endif
  1173.     }
  1174.       /* Extracting a single bit from the result of a shift:
  1175.      see which bit it was before the shift and extract that directly.  */
  1176.       if (was_replaced[0]
  1177.       && (GET_CODE (to) == ASHIFTRT || GET_CODE (to) == LSHIFTRT
  1178.           || GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1179.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1180.       && XEXP (x, 1) == const1_rtx
  1181.       && GET_CODE (XEXP (x, 2)) == CONST_INT)
  1182.     {
  1183.       int shift = INTVAL (XEXP (to, 1));
  1184.       int newpos;
  1185.       if (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1186.         shift = - shift;
  1187. #ifdef BITS_BIG_ENDIAN
  1188.       shift = - shift;
  1189. #endif
  1190.       newpos = INTVAL (XEXP (x, 2)) + shift;
  1191.       if (newpos >= 0 &&
  1192.           newpos < GET_MODE_BITSIZE (GET_MODE (to)))
  1193.         {
  1194.           if (!undobuf.storage)
  1195.         undobuf.storage = (char *) oballoc (0);
  1196.           return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
  1197.                   XEXP (to, 0), const1_rtx,
  1198.                   gen_rtx (CONST_INT, VOIDmode, newpos));
  1199.         }
  1200.     }
  1201.       break;
  1202.  
  1203.     case LSHIFTRT:
  1204.     case ASHIFTRT:
  1205.     case ROTATE:
  1206.     case ROTATERT:
  1207. #ifdef SHIFT_COUNT_TRUNCATED
  1208.       /* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
  1209.      True for all kinds of shifts and also for zero_extend.  */
  1210.       if (was_replaced[1]
  1211.       && (GET_CODE (to) == SIGN_EXTEND
  1212.           || GET_CODE (to) == ZERO_EXTEND)
  1213.       && FAKE_EXTEND_SAFE_P (GET_MODE (to), XEXP (to, 0)))
  1214.     {
  1215.       if (!undobuf.storage)
  1216.         undobuf.storage = (char *) oballoc (0);
  1217.       SUBST (XEXP (x, 1),
  1218.          /* This is a perverse SUBREG, wider than its base.  */
  1219.          gen_lowpart_for_combine (GET_MODE (to), XEXP (to, 0)));
  1220.     }
  1221. #endif
  1222.       /* Two shifts in a row of same kind
  1223.      in same direction with constant counts
  1224.      may be combined.  */
  1225.       if (was_replaced[0]
  1226.       && GET_CODE (to) == GET_CODE (x)
  1227.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  1228.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1229.       && INTVAL (XEXP (to, 1)) > 0
  1230.       && INTVAL (XEXP (x, 1)) > 0
  1231.       && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
  1232.           < GET_MODE_BITSIZE (GET_MODE (x))))
  1233.     {
  1234.       if (!undobuf.storage)
  1235.         undobuf.storage = (char *) oballoc (0);
  1236.       return gen_rtx (GET_CODE (x), GET_MODE (x),
  1237.               XEXP (to, 0),
  1238.               gen_rtx (CONST_INT, VOIDmode,
  1239.                    INTVAL (XEXP (x, 1))
  1240.                    + INTVAL (XEXP (to, 1))));
  1241.     }
  1242.       break;
  1243.  
  1244.     case LSHIFT:
  1245.     case ASHIFT:
  1246. #ifdef SHIFT_COUNT_TRUNCATED
  1247.       /* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
  1248.      True for all kinds of shifts and also for zero_extend.  */
  1249.       if (was_replaced[1]
  1250.       && (GET_CODE (to) == SIGN_EXTEND
  1251.           || GET_CODE (to) == ZERO_EXTEND)
  1252.       && GET_CODE (to) == REG)
  1253.     {
  1254.       if (!undobuf.storage)
  1255.         undobuf.storage = (char *) oballoc (0);
  1256.       SUBST (XEXP (x, 1), gen_rtx (SUBREG, GET_MODE (to), XEXP (to, 0), 0));
  1257.     }
  1258. #endif
  1259.       /* (lshift (and (lshiftrt <foo> <X>) <Y>) <X>)
  1260.      happens copying between bit fields in similar structures.
  1261.      It can be replaced by one and instruction.
  1262.      It does not matter whether the shifts are logical or arithmetic.  */
  1263.       if (GET_CODE (XEXP (x, 0)) == AND
  1264.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  1265.       && INTVAL (XEXP (x, 1)) > 0
  1266.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1267.       && XEXP (XEXP (x, 0), 0) == to
  1268.       && (GET_CODE (to) == LSHIFTRT
  1269.           || GET_CODE (to) == ASHIFTRT)
  1270. #if 0
  1271. /* I now believe this restriction is unnecessary.
  1272.    The outer shift will discard those bits in any case, right?  */
  1273.  
  1274.           /* If inner shift is arithmetic, either it shifts left or
  1275.          the bits it shifts the sign into are zeroed by the and.  */
  1276.           && (INTVAL (XEXP (x, 1)) < 0
  1277.               || ((unsigned) INTVAL (XEXP (XEXP (x, 0), 1))
  1278.               < 1 << (GET_MODE_BITSIZE (GET_MODE (x))
  1279.                   - INTVAL (XEXP (x, 0)))))
  1280. #endif
  1281.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1282.       && INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
  1283.     {
  1284.       if (!undobuf.storage)
  1285.         undobuf.storage = (char *) oballoc (0);
  1286.       /* The constant in the new `and' is <Y> << <X>
  1287.          but clear out all bits that don't belong in our mode.  */
  1288.       return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
  1289.               gen_rtx (CONST_INT, VOIDmode,
  1290.                    (GET_MODE_MASK (GET_MODE (x))
  1291.                     & ((GET_MODE_MASK (GET_MODE (x))
  1292.                     & INTVAL (XEXP (XEXP (x, 0), 1)))
  1293.                        << INTVAL (XEXP (x, 1))))));
  1294.     } 
  1295.       /* Two shifts in a row in same direction with constant counts
  1296.      may be combined.  */
  1297.       if (was_replaced[0]
  1298.       && (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1299.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  1300.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1301.       && INTVAL (XEXP (to, 1)) > 0
  1302.       && INTVAL (XEXP (x, 1)) > 0
  1303.       && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
  1304.           < GET_MODE_BITSIZE (GET_MODE (x))))
  1305.     {
  1306.       if (!undobuf.storage)
  1307.         undobuf.storage = (char *) oballoc (0);
  1308.       return gen_rtx (GET_CODE (x), GET_MODE (x),
  1309.               XEXP (to, 0),
  1310.               gen_rtx (CONST_INT, VOIDmode,
  1311.                    INTVAL (XEXP (x, 1))
  1312.                    + INTVAL (XEXP (to, 1))));
  1313.     }
  1314.       /* (ashift (ashiftrt <foo> <X>) <X>)
  1315.      (or, on some machines, (ashift (ashift <foo> <-X>) <X>) instead)
  1316.      happens if you divide by 2**N and then multiply by 2**N.
  1317.      It can be replaced by one `and' instruction.
  1318.      It does not matter whether the shifts are logical or arithmetic.  */
  1319.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  1320.       && INTVAL (XEXP (x, 1)) > 0
  1321.       && was_replaced[0]
  1322.       && (((GET_CODE (to) == LSHIFTRT || GET_CODE (to) == ASHIFTRT)
  1323.            && GET_CODE (XEXP (to, 1)) == CONST_INT
  1324.            && INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
  1325.           ||
  1326.           ((GET_CODE (to) == LSHIFT || GET_CODE (to) == ASHIFT)
  1327.            && GET_CODE (XEXP (to, 1)) == CONST_INT
  1328.            && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (to, 1)))))
  1329.     {
  1330.       if (!undobuf.storage)
  1331.         undobuf.storage = (char *) oballoc (0);
  1332.       /* The constant in the new `and' is -1 << <X>
  1333.          but clear out all bits that don't belong in our mode.  */
  1334.       return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
  1335.               gen_rtx (CONST_INT, VOIDmode,
  1336.                    (GET_MODE_MASK (GET_MODE (x))
  1337.                     & (GET_MODE_MASK (GET_MODE (x))
  1338.                        << INTVAL (XEXP (x, 1))))));
  1339.     } 
  1340.  
  1341.     }
  1342.  
  1343.   return x;
  1344. }
  1345.  
  1346. /* This is the AND case of the function subst.  */
  1347.  
  1348. static rtx
  1349. simplify_and_const_int (x, to)
  1350.      rtx x, to;
  1351. {
  1352.   register rtx varop = XEXP (x, 0);
  1353.   register int constop = INTVAL (XEXP (x, 1));
  1354.  
  1355.   /* (and (subreg (and <foo> <constant>) 0) <constant>)
  1356.      results from an andsi followed by an andqi,
  1357.      which happens frequently when storing bit-fields
  1358.      on something whose result comes from an andsi.  */
  1359.   if (GET_CODE (varop) == SUBREG
  1360.       && XEXP (varop, 0) == to
  1361.       && subreg_lowpart_p (varop)
  1362.       && GET_CODE (to) == AND
  1363.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1364.       /* Verify that the result of the outer `and'
  1365.      is not affected by any bits not defined in the inner `and'.
  1366.      True if the outer mode is narrower, or if the outer constant
  1367.      masks to zero all the bits that the inner mode doesn't have.  */
  1368.       && (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (to))
  1369.       || (constop & ~ GET_MODE_MASK (GET_MODE (to))) == 0))
  1370.     {
  1371.       if (!undobuf.storage)
  1372.     undobuf.storage = (char *) oballoc (0);
  1373.       return gen_rtx (AND, GET_MODE (x),
  1374.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1375.               gen_rtx (CONST_INT, VOIDmode,
  1376.                    constop
  1377.                    /* Remember that the bits outside that mode
  1378.                   are not being changed, so the effect
  1379.                   is as if they were all 1.  */
  1380.                    & INTVAL (XEXP (to, 1))));
  1381.     } 
  1382.   /* (and:SI (zero_extract:SI ...) <constant>)
  1383.      results from an andsi following a byte-fetch on risc machines.
  1384.      When the constant includes all bits extracted, eliminate the `and'.  */
  1385.   if (GET_CODE (varop) == ZERO_EXTRACT
  1386.       && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1387.       /* The `and' must not clear any bits that the extract can give.  */
  1388.       && (~ constop & ((1 << INTVAL (XEXP (varop, 1))) - 1)) == 0)
  1389.     return varop;
  1390.   /* (and (zero_extend <foo>) <constant>)
  1391.      often results from storing in a bit-field something
  1392.      that was calculated as a short.  Replace with a single `and'
  1393.      in whose constant all bits not in <foo>'s mode are zero.  */
  1394.   if (varop == to
  1395.       && GET_CODE (to) == ZERO_EXTEND
  1396.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1397.     {
  1398.       if (!undobuf.storage)
  1399.     undobuf.storage = (char *) oballoc (0);
  1400.       return gen_rtx (AND, GET_MODE (x),
  1401.               /* This is a perverse SUBREG, wider than its base.  */
  1402.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1403.               gen_rtx (CONST_INT, VOIDmode,
  1404.                    constop & GET_MODE_MASK (GET_MODE (XEXP (to, 0)))));
  1405.     }
  1406.   /* (and (sign_extend <foo>) <constant>)
  1407.      can be replaced with (and (subreg <foo>) <constant>)
  1408.      if <constant> is narrower than <foo>'s mode,
  1409.      or with (zero_extend <foo>) if <constant> is a mask for that mode.  */
  1410.   if (varop == to
  1411.       && GET_CODE (to) == SIGN_EXTEND
  1412.       && ((unsigned) constop <= GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
  1413.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1414.     {
  1415.       if (!undobuf.storage)
  1416.     undobuf.storage = (char *) oballoc (0);
  1417.       if (constop == GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
  1418.     return gen_rtx (ZERO_EXTEND, GET_MODE (x), XEXP (to, 0));
  1419.       return gen_rtx (AND, GET_MODE (x),
  1420.               /* This is a perverse SUBREG, wider than its base.  */
  1421.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1422.               XEXP (x, 1));
  1423.     }
  1424.   /* (and (and <foo> <constant>) <constant>)
  1425.      comes from two and instructions in a row.  */
  1426.   if (varop == to
  1427.       && GET_CODE (to) == AND
  1428.       && GET_CODE (XEXP (to, 1)) == CONST_INT)
  1429.     {
  1430.       if (!undobuf.storage)
  1431.     undobuf.storage = (char *) oballoc (0);
  1432.       return gen_rtx (AND, GET_MODE (x),
  1433.               XEXP (to, 0),
  1434.               gen_rtx (CONST_INT, VOIDmode,
  1435.                    constop
  1436.                    & INTVAL (XEXP (to, 1))));
  1437.     }
  1438.   /* (and (ashiftrt (ashift FOO N) N) CONST)
  1439.      may be simplified to (and FOO CONST) if CONST masks off the bits
  1440.      changed by the two shifts.  */
  1441.   if (GET_CODE (varop) == ASHIFTRT
  1442.       && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1443.       && XEXP (varop, 0) == to
  1444.       && GET_CODE (to) == ASHIFT
  1445.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1446.       && INTVAL (XEXP (varop, 1)) == INTVAL (XEXP (to, 1))
  1447.       && ((unsigned) constop >> INTVAL (XEXP (varop, 1))) == 0)
  1448.     {
  1449.       if (!undobuf.storage)
  1450.     undobuf.storage = (char *) oballoc (0);
  1451.       /* If CONST is a mask for the low byte,
  1452.      change this into a zero-extend instruction
  1453.      from just the low byte of FOO.  */
  1454.       if (constop == GET_MODE_MASK (QImode))
  1455.     {
  1456.       rtx temp = gen_lowpart_for_combine (QImode, XEXP (to, 0));
  1457.       if (GET_CODE (temp) != CLOBBER)
  1458.         return gen_rtx (ZERO_EXTEND, GET_MODE (x), temp);
  1459.     }
  1460.       return gen_rtx (AND, GET_MODE (x),
  1461.               XEXP (to, 0), XEXP (x, 1));
  1462.     }
  1463.   /* (and x const) may be converted to (zero_extend (subreg x 0)).  */
  1464.   if (constop == GET_MODE_MASK (QImode)
  1465.       && GET_CODE (varop) == REG)
  1466.     {
  1467.       if (!undobuf.storage)
  1468.     undobuf.storage = (char *) oballoc (0);
  1469.       return gen_rtx (ZERO_EXTEND, GET_MODE (x),
  1470.               gen_rtx (SUBREG, QImode, varop, 0));
  1471.     }
  1472.   if (constop == GET_MODE_MASK (HImode)
  1473.       && GET_CODE (varop) == REG)
  1474.     {
  1475.       if (!undobuf.storage)
  1476.     undobuf.storage = (char *) oballoc (0);
  1477.       return gen_rtx (ZERO_EXTEND, GET_MODE (x),
  1478.               gen_rtx (SUBREG, HImode, varop, 0));
  1479.     }
  1480.   /* No simplification applies.  */
  1481.   return 0;
  1482. }
  1483.  
  1484. /* Like gen_lowpart but for use by combine.  In combine it is not possible
  1485.    to create any new pseudoregs.  However, it is safe to create
  1486.    invalid memory addresses, because combine will try to recognize
  1487.    them and all they will do is make the combine attempt fail.
  1488.  
  1489.    If for some reason this cannot do its job, an rtx
  1490.    (clobber (const_int 0)) is returned.
  1491.    An insn containing that will not be recognized.  */
  1492.  
  1493. #undef gen_lowpart
  1494.  
  1495. static rtx
  1496. gen_lowpart_for_combine (mode, x)
  1497.      enum machine_mode mode;
  1498.      register rtx x;
  1499. {
  1500.   if (GET_CODE (x) == SUBREG || GET_CODE (x) == REG)
  1501.     return gen_lowpart (mode, x);
  1502.   if (GET_MODE (x) == mode || x->volatil)
  1503.     return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1504.   if (GET_CODE (x) == MEM)
  1505.     {
  1506.       register int offset = 0;
  1507. #ifdef WORDS_BIG_ENDIAN
  1508.       offset = (max (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
  1509.         - max (GET_MODE_SIZE (mode), UNITS_PER_WORD));
  1510. #endif
  1511. #ifdef BYTES_BIG_ENDIAN
  1512.       /* Adjust the address so that the address-after-the-data
  1513.      is unchanged.  */
  1514.       offset -= (min (UNITS_PER_WORD, GET_MODE_SIZE (mode))
  1515.          - min (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
  1516. #endif
  1517.       return gen_rtx (MEM, mode, plus_constant (XEXP (x, 0),
  1518.                         offset));
  1519.     }
  1520.   else
  1521.     return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1522. }
  1523.  
  1524. /* After substitution, if the resulting pattern looks like
  1525.    (set (cc0) (and ...)) or (set (cc0) (lshiftrt ...)),
  1526.    this function is called to simplify the
  1527.    pattern into a bit-field operation if possible.  */
  1528.  
  1529. static void
  1530. simplify_set_cc0_and (insn)
  1531.      rtx insn;
  1532. {
  1533.   register rtx value = XEXP (PATTERN (insn), 1);
  1534.   register rtx op0 = XEXP (value, 0);
  1535.   register rtx op1 = XEXP (value, 1);
  1536.   int offset = 0;
  1537.   rtx var = 0;
  1538.   rtx bitnum = 0;
  1539.   int temp;
  1540.   int unit;
  1541.   rtx newpat;
  1542.  
  1543.   if (GET_CODE (value) == AND)
  1544.     {
  1545.       op0 = XEXP (value, 0);
  1546.       op1 = XEXP (value, 1);
  1547.     }
  1548.   else if (GET_CODE (value) == LSHIFTRT)
  1549.     {
  1550.       /* If there is no AND, but there is a shift that discards
  1551.      all but the sign bit, we can pretend that the shift result
  1552.      is ANDed with 1.  Otherwise we cannot handle just a shift.  */
  1553.       if (GET_CODE (XEXP (value, 1)) == CONST_INT
  1554.       && (INTVAL (XEXP (value, 1))
  1555.           == GET_MODE_BITSIZE (GET_MODE (value)) - 1))
  1556.     {
  1557.       op0 = value;
  1558.       op1 = const1_rtx;
  1559.     }
  1560.       else
  1561.     return;
  1562.     }
  1563.   else
  1564.     abort ();
  1565.  
  1566.   /* Look for a constant power of 2 or a shifted 1
  1567.      on either side of the AND.  Set VAR to the other side.
  1568.      Set BITNUM to the shift count of the 1 (as an rtx).
  1569.      Or, if bit number is constant, set OFFSET to the bit number.  */
  1570.  
  1571.   switch (GET_CODE (op0))
  1572.     {
  1573.     case CONST_INT:
  1574.       temp = exact_log2 (INTVAL (op0));
  1575.       if (temp < 0)
  1576.     return;
  1577.       offset = temp;
  1578.       var = op1;
  1579.       break;
  1580.  
  1581.     case ASHIFT:
  1582.     case LSHIFT:
  1583.       if (XEXP (op0, 0) == const1_rtx)
  1584.     {
  1585.       bitnum = XEXP (op0, 1);
  1586.       var = op1;
  1587.     }
  1588.     }
  1589.   if (var == 0)
  1590.     switch (GET_CODE (op1))
  1591.       {
  1592.       case CONST_INT:
  1593.     temp = exact_log2 (INTVAL (op1));
  1594.     if (temp < 0)
  1595.       return;
  1596.     offset = temp;
  1597.     var = op0;
  1598.     break;
  1599.  
  1600.       case ASHIFT:
  1601.       case LSHIFT:
  1602.     if (XEXP (op1, 0) == const1_rtx)
  1603.       {
  1604.         bitnum = XEXP (op1, 1);
  1605.         var = op0;
  1606.       }
  1607.       }
  1608.  
  1609.   /* If VAR is 0, we didn't find something recognizable.  */
  1610.   if (var == 0)
  1611.     return;
  1612.  
  1613.   if (!undobuf.storage)
  1614.     undobuf.storage = (char *) oballoc (0);
  1615.  
  1616.   /* If the bit position is currently exactly 0,
  1617.      extract a right-shift from the variable portion.  */
  1618.   if (offset == 0
  1619.       && (GET_CODE (var) == ASHIFTRT || GET_CODE (var) == LSHIFTRT))
  1620.     {
  1621.       bitnum = XEXP (var, 1);
  1622.       var = XEXP (var, 0);
  1623.     }
  1624.  
  1625.   if (GET_CODE (var) == SUBREG && SUBREG_WORD (var) == 0)
  1626.     var = SUBREG_REG (var);
  1627.  
  1628.   /* Note that BITNUM and OFFSET are always little-endian thru here
  1629.      even on a big-endian machine.  */
  1630.  
  1631. #ifdef BITS_BIG_ENDIAN
  1632.   unit = GET_MODE_BITSIZE (GET_MODE (var)) - 1;
  1633.  
  1634.   if (bitnum != 0)
  1635.     bitnum = gen_rtx (MINUS, SImode,
  1636.               gen_rtx (CONST_INT, VOIDmode, unit), bitnum);
  1637.   else
  1638.     offset = unit - offset;
  1639. #endif
  1640.  
  1641.   if (bitnum == 0)
  1642.     bitnum = gen_rtx (CONST_INT, VOIDmode, offset);
  1643.  
  1644.   newpat = gen_rtx (SET, VOIDmode, cc0_rtx,
  1645.             gen_rtx (ZERO_EXTRACT, VOIDmode, var, const1_rtx, bitnum));
  1646.   if (recog (newpat, insn) >= 0)
  1647.     {
  1648.       if (undobuf.num_undo < MAX_UNDO)
  1649.     {
  1650.       undobuf.undo[undobuf.num_undo].where = &XEXP (PATTERN (insn), 1);
  1651.       undobuf.undo[undobuf.num_undo].old_contents = value;
  1652.       XEXP (PATTERN (insn), 1) = XEXP (newpat, 1);
  1653.     }
  1654.       undobuf.num_undo++;
  1655.     }
  1656. }
  1657.  
  1658. /* Update the records of when each REG was most recently set or killed
  1659.    for the things done by INSN.  This is the last thing done in processing
  1660.    INSN in the combiner loop.
  1661.  
  1662.    We update reg_last_set, reg_last_death, and also the similar information
  1663.    mem_last_set (which insn most recently modified memory)
  1664.    and last_call_cuid (which insn was the most recent subroutine call).  */
  1665.  
  1666. static void
  1667. record_dead_and_set_regs (insn)
  1668.      rtx insn;
  1669. {
  1670.   register rtx link;
  1671.   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1672.     {
  1673.       if (REG_NOTE_KIND (link) == REG_DEAD)
  1674.     reg_last_death[REGNO (XEXP (link, 0))] = insn;
  1675.       else if (REG_NOTE_KIND (link) == REG_INC)
  1676.     reg_last_set[REGNO (XEXP (link, 0))] = insn;
  1677.     }
  1678.  
  1679.   if (GET_CODE (insn) == CALL_INSN)
  1680.     last_call_cuid = mem_last_set = INSN_CUID (insn);
  1681.  
  1682.   if (GET_CODE (PATTERN (insn)) == PARALLEL)
  1683.     {
  1684.       register int i;
  1685.       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  1686.     {
  1687.       register rtx elt = XVECEXP (PATTERN (insn), 0, i);
  1688.       register enum rtx_code code = GET_CODE (elt);
  1689.       if (code == SET || code == CLOBBER)
  1690.         {
  1691.           if (GET_CODE (XEXP (elt, 0)) == REG)
  1692.         reg_last_set[REGNO (XEXP (elt, 0))] = insn;
  1693.           if (GET_CODE (XEXP (elt, 0)) == SUBREG
  1694.           && GET_CODE (SUBREG_REG (XEXP (elt, 0))) == REG)
  1695.         reg_last_set[REGNO (SUBREG_REG (XEXP (elt, 0)))] = insn;
  1696.           else if (GET_CODE (XEXP (elt, 0)) == MEM)
  1697.         mem_last_set = INSN_CUID (insn);
  1698.         }
  1699.     }
  1700.     }
  1701.   else if (GET_CODE (PATTERN (insn)) == SET
  1702.        || GET_CODE (PATTERN (insn)) == CLOBBER)
  1703.     {
  1704.       register rtx x = XEXP (PATTERN (insn), 0);
  1705.       if (GET_CODE (x) == REG)
  1706.     reg_last_set[REGNO (x)] = insn;
  1707.       if (GET_CODE (x) == SUBREG
  1708.       && GET_CODE (SUBREG_REG (x)) == REG)
  1709.     reg_last_set[REGNO (SUBREG_REG (x))] = insn;
  1710.       else if (GET_CODE (x) == MEM)
  1711.     mem_last_set = INSN_CUID (insn);
  1712.     }
  1713. }
  1714.  
  1715. /* Return nonzero if expression X refers to a REG or to memory
  1716.    that is set in an instruction more recent than FROM_CUID.  */
  1717.  
  1718. static int
  1719. use_crosses_set_p (x, from_cuid)
  1720.      register rtx x;
  1721.      int from_cuid;
  1722. {
  1723.   register char *fmt;
  1724.   register int i;
  1725.   register enum rtx_code code = GET_CODE (x);
  1726.  
  1727.   if (code == REG)
  1728.     {
  1729.       register int regno = REGNO (x);
  1730.       return (reg_last_set[regno]
  1731.           && INSN_CUID (reg_last_set[regno]) > from_cuid);
  1732.     }
  1733.  
  1734.   if (code == MEM && mem_last_set > from_cuid)
  1735.     return 1;
  1736.  
  1737.   fmt = GET_RTX_FORMAT (code);
  1738.  
  1739.   for (i = GET_RTX_LENGTH (code); i >= 0; i--)
  1740.     {
  1741.       if (fmt[i] == 'E')
  1742.     {
  1743.       register int j;
  1744.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1745.         if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
  1746.           return 1;
  1747.     }
  1748.       else if (fmt[i] == 'e'
  1749.            && use_crosses_set_p (XEXP (x, i), from_cuid))
  1750.     return 1;
  1751.     }
  1752.   return 0;
  1753. }
  1754.  
  1755. /* Return nonzero if reg REGNO is marked as dying in INSN.  */
  1756.  
  1757. int
  1758. regno_dead_p (regno, insn)
  1759.      int regno;
  1760.      rtx insn;
  1761. {
  1762.   register rtx link;
  1763.  
  1764.   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1765.     if ((REG_NOTE_KIND (link) == REG_DEAD
  1766.      || REG_NOTE_KIND (link) == REG_INC)
  1767.     && REGNO (XEXP (link, 0)) == regno)
  1768.       return 1;
  1769.  
  1770.   return 0;
  1771. }
  1772.  
  1773. /* Remove register number REGNO from the dead registers list of INSN.  */
  1774.  
  1775. void
  1776. remove_death (regno, insn)
  1777.      int regno;
  1778.      rtx insn;
  1779. {
  1780.   register rtx link, next;
  1781.   while ((link = REG_NOTES (insn))
  1782.      && REG_NOTE_KIND (link) == REG_DEAD
  1783.      && REGNO (XEXP (link, 0)) == regno)
  1784.     REG_NOTES (insn) = XEXP (link, 1);
  1785.  
  1786.   if (link)
  1787.     while (next = XEXP (link, 1))
  1788.       {
  1789.     if (REG_NOTE_KIND (next) == REG_DEAD
  1790.         && REGNO (XEXP (next, 0)) == regno)
  1791.       XEXP (link, 1) = XEXP (next, 1);
  1792.     else
  1793.       link = next;
  1794.       }
  1795. }
  1796.  
  1797. /* Return nonzero if J is the first insn following I,
  1798.    not counting labels, line numbers, etc.
  1799.    We assume that J follows I.  */
  1800.  
  1801. static int
  1802. adjacent_insns_p (i, j)
  1803.      rtx i, j;
  1804. {
  1805.   register rtx insn;
  1806.   for (insn = NEXT_INSN (i); insn != j; insn = NEXT_INSN (insn))
  1807.     if (GET_CODE (insn) == INSN
  1808.     || GET_CODE (insn) == CALL_INSN
  1809.     || GET_CODE (insn) == JUMP_INSN)
  1810.       return 0;
  1811.   return 1;
  1812. }
  1813.  
  1814. /* Concatenate the list of logical links of OINSN
  1815.    into INSN's list of logical links.
  1816.    Modifies OINSN destructively.
  1817.  
  1818.    If ALL_LINKS is nonzero, move all the links that OINSN has.
  1819.    Otherwise, move only those that point to insns that set regs
  1820.    that die in the insn OINSN.
  1821.    Other links are clobbered so that they are no longer effective.  */
  1822.  
  1823. static void
  1824. add_links (insn, oinsn, all_links)
  1825.      rtx insn, oinsn;
  1826.      int all_links;
  1827. {
  1828.   register rtx links = LOG_LINKS (oinsn);
  1829.   if (! all_links)
  1830.     {
  1831.       rtx tail;
  1832.       for (tail = links; tail; tail = XEXP (tail, 1))
  1833.     {
  1834.       rtx target = XEXP (tail, 0);
  1835.       if (GET_CODE (target) != INSN
  1836.           || GET_CODE (PATTERN (target)) != SET
  1837.           || GET_CODE (SET_DEST (PATTERN (target))) != REG
  1838.           || ! dead_or_set_p (oinsn, SET_DEST (PATTERN (target))))
  1839.         /* OINSN is going to become a NOTE 
  1840.            so a link pointing there will have no effect.  */
  1841.         XEXP (tail, 0) = oinsn;
  1842.     }
  1843.     }
  1844.   if (LOG_LINKS (insn) == 0)
  1845.     LOG_LINKS (insn) = links;
  1846.   else
  1847.     {
  1848.       register rtx next, prev = LOG_LINKS (insn);
  1849.       while (next = XEXP (prev, 1))
  1850.     prev = next;
  1851.       XEXP (prev, 1) = links;
  1852.     }
  1853. }
  1854.  
  1855. /* Concatenate the any elements of the list of reg-notes INCS
  1856.    which are of type REG_INC
  1857.    into INSN's list of reg-notes.  */
  1858.  
  1859. static void
  1860. add_incs (insn, incs)
  1861.      rtx insn, incs;
  1862. {
  1863.   register rtx tail;
  1864.  
  1865.   for (tail = incs; tail; tail = XEXP (tail, 1))
  1866.     if (REG_NOTE_KIND (tail) == REG_INC)
  1867.       REG_NOTES (insn)
  1868.     = gen_rtx (EXPR_LIST, REG_INC, XEXP (tail, 0), REG_NOTES (insn));
  1869. }
  1870.  
  1871. /* For each register (hardware or pseudo) used within expression X,
  1872.    if its death is in an instruction with cuid
  1873.    between FROM_CUID (inclusive) and TO_INSN (exclusive),
  1874.    mark it as dead in TO_INSN instead.
  1875.  
  1876.    This is done when X is being merged by combination into TO_INSN.  */
  1877.  
  1878. static void
  1879. move_deaths (x, from_cuid, to_insn)
  1880.      rtx x;
  1881.      int from_cuid;
  1882.      rtx to_insn;
  1883. {
  1884.   register char *fmt;
  1885.   register int len, i;
  1886.   register enum rtx_code code = GET_CODE (x);
  1887.  
  1888.   if (code == REG)
  1889.     {
  1890.       register rtx where_dead = reg_last_death[REGNO (x)];
  1891.  
  1892.       if (where_dead && INSN_CUID (where_dead) >= from_cuid
  1893.       && INSN_CUID (where_dead) < INSN_CUID (to_insn))
  1894.     {
  1895.       remove_death (REGNO (x), reg_last_death[REGNO (x)]);
  1896.       if (! dead_or_set_p (to_insn, x))
  1897.         REG_NOTES (to_insn)
  1898.           = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
  1899.     }
  1900.       return;
  1901.     }
  1902.  
  1903.   len = GET_RTX_LENGTH (code);
  1904.   fmt = GET_RTX_FORMAT (code);
  1905.  
  1906.   for (i = 0; i < len; i++)
  1907.     {
  1908.       if (fmt[i] == 'E')
  1909.     {
  1910.       register int j;
  1911.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1912.         move_deaths (XVECEXP (x, i, j), from_cuid, to_insn);
  1913.     }
  1914.       else if (fmt[i] == 'e')
  1915.     move_deaths (XEXP (x, i), from_cuid, to_insn);
  1916.     }
  1917. }
  1918.  
  1919. void
  1920. dump_combine_stats (file)
  1921.      char *file;
  1922. {
  1923.   fprintf
  1924.     (file,
  1925.      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n"
  1926.      , combine_attempts, combine_merges, combine_extras, combine_successes);
  1927. }
  1928.  
  1929. void
  1930. dump_combine_total_stats (file)
  1931.      char *file;
  1932. {
  1933.   fprintf
  1934.     (file,
  1935.      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
  1936.      total_attempts, total_merges, total_extras, total_successes);
  1937. }
  1938.